<template>
  <div class="content">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-select
          v-model="searchParams.contentCateId"
          placeholder="请选择内容分类"
          clearable
          size="mini"
        >
          <el-option
            v-for="item in typeArr"
            :key="item.id"
            :value="item.id"
            :label="item.title"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('contents/index')"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('contents/index')"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="add"
          v-if="has('contents/store')"
          >添加</el-button
        >
      </el-row>

      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
          sortable
        ></el-table-column>
        <el-table-column label="所属分类" width="300" align="center">
          <template slot-scope="scope">
            {{ scope.row.cate.name }}
          </template>
        </el-table-column>
        <el-table-column label="内容" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.content }}
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
          sortable
        ></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row.id)"
              v-if="has('contents/show')"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id, scope.$index)"
              v-if="has('contents/destroy')"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <!-- 添加 -->
    <el-card v-if="status === 'add'">
      <div slot="header">
        <el-page-header @back="init" content="添加内容"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="内容分类" prop="content_cate_id">
          <el-select
            v-model="editParams.content_cate_id"
            placeholder="内容分类"
          >
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Editor v-model="editParams.content"></Editor>
        </el-form-item>

        <el-form-item>
          <el-button @click="init">取 消</el-button>
          <el-button
            type="primary"
            @click="submit('form', 1)"
            v-if="has('contents/store')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 编辑 -->
    <el-card v-if="status === 'edit'">
      <div slot="header">
        <el-page-header @back="init" content="编辑内容"></el-page-header>
      </div>
      <el-form
        :model="editParams"
        :rules="rules"
        ref="form"
        label-width="120px"
      >
        <el-form-item label="内容分类" prop="content_cate_id">
          <el-select
            v-model="editParams.content_cate_id"
            placeholder="内容分类"
          >
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容内容" prop="content">
          <Editor v-model="editParams.content"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button @click="init">取 消</el-button>
          <el-button
            type="primary"
            @click="submit('form', 2)"
            v-if="has('contents/update')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Editor from "@/components/Editer/index.vue";
import api from "../../api/api.js";
export default {
  name: "content",
  components: {
    Editor
  },
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      typeArr: [],
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        contentCateId: ""
      },
      editParams: {
        // 添加/编辑参数
        content_cate_id: "",
        content: ""
      },

      rules: {
        // 验证规则
        content_cate_id: [
          { required: true, message: "请选择内容分类", trigger: "change" }
        ],
        content: [
          { required: true, message: "请输入内容内容", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCate();
    this.getData();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },

    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有分类
    getData() {
      this.table_loading = true;
      api.contents
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getCate() {
      api.common
        .contentCate()
        .then(res => {
          if (res.data.code == 200) {
            this.typeArr = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.contentCateId = "";
      this.editParams = {
        content_cate_id: "",
        content: ""
      };
      this.status = "index";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    add() {
      this.status = "add";
    },
    // 编辑页面
    edit(id) {
      this.load();
      api.contents
        .show(id)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.status = "edit";
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 保存添加或编辑
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          this.load();
          // 添加
          if (type === 1) {
            api.contents
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.$message.success(res.data.msg);
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type == 2) {
            // 编辑
            api.contents
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.$message.success(res.data.msg);
                  this.reset();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    // 删除
    del(id, index) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          api.contents
            .del(id)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.content {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
